package ru.bragnikita.utils.swing.widget;

import ru.bragnikita.utils.swing.widget.field.Field;

import javax.swing.*;
import java.awt.*;

/**
 * @author Nikita.Brazhnikov
 */
public class WtTextField extends WtAbstractTextWidget<JTextField> implements WtTextWidget<JTextField> {

    private Field description;

    public WtTextField(Field description) {
        super(description.getId());
        this.description = description;
        setLabel(new JLabel(description.getName()));
        JTextField field = new JTextField(description.getLength());
        field.setMinimumSize(new Dimension(field.getPreferredSize().width / 2, field.getPreferredSize().height));
        setComponent(field);
    }

    public WtTextField text(String text) {
        getComponent().setText(text);
        return this;
    }

    public Field getDescription() {
        return description;
    }


    @Override
    public String getAsString() {
        return description.getAsString(getComponent().getText());
    }

    @Override
    public String getValue() {
        return getAsString();
    }

    @Override
    public void setValue(Object value) {
        getComponent().setText(description.getAsString(value));
    }

}
